package top.leinx.tmind;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.util.StringConverter;
import top.leinx.tmind.component.Config;
import top.leinx.tmind.component.MindSubject;
import top.leinx.tmind.component.MindSubjects;

public class Controller implements Initializable {
    private MindSubject centerSubject = null;
    private MindSubject selectedSubject = null;

    @FXML
    private Pane drawPane;
    @FXML
    private ToolBar toolbar;
    @FXML
    private TextField textField;

    @FXML
    public void addCenter() {
        centerSubject = new MindSubject(drawPane, null, "中心主题", Config.LAYOUT_RIGHT); // 创建中心主题
        selectedSubject = centerSubject;
        selectedSubject.setSelected(true);

        centerSubject.getLabel().setOnMouseClicked(event -> {
            if (selectedSubject != null) {
                selectedSubject.setSelected(false);
            }
            selectedSubject = (MindSubject) ((Label) event.getSource()).getUserData();
            selectedSubject.setSelected(true);
            textField.setText(selectedSubject.getText());
        });

        drawPane.getChildren().add(centerSubject.getLabel());

        centerSubject.setX((drawPane.getWidth() - centerSubject.getLabel().getWidth()) / 2.0);
        centerSubject.setY((drawPane.getHeight() - centerSubject.getLabel().getHeight()) / 2.0);
    }

    @FXML
    public void addSubject() {
        if (selectedSubject == null) {
            return;
        }

        String text = "子主题" + (int) (Math.random() * 100);

        MindSubject subject = new MindSubject(drawPane, selectedSubject, text, Config.LAYOUT_RIGHT); // 创建子主题
        subject.getLabel().setOnMouseClicked(event -> {
            if (selectedSubject != null) {
                selectedSubject.setSelected(false);
            }
            selectedSubject = (MindSubject) ((Label) event.getSource()).getUserData();
            selectedSubject.setSelected(true);
            textField.setText(selectedSubject.getText());
        });

        selectedSubject.getItems().add(subject);

        System.out.println(subject.isSelected());
        drawPane.getChildren().add(subject.getLabel());
        MindSubjects.adjustPosition(centerSubject, subject.getPostion(), centerSubject.getLabel().getHeight());

        Line line = new Line();
        Label parentLabel = subject.getParent().getLabel();
        if (subject.getPostion() == Config.LAYOUT_RIGHT) {
            line.startXProperty().bind(
                    parentLabel.layoutXProperty().add(parentLabel.widthProperty())
            );
            line.startYProperty().bind(
                    parentLabel.layoutYProperty().add(parentLabel.heightProperty().divide(2))
            );
            line.endXProperty().bind(subject.getLabel().layoutXProperty());
            line.endYProperty().bind(
                    subject.getLabel().layoutYProperty().add(subject.getLabel().heightProperty().divide(2))
            );
        }

        drawPane.getChildren().add(line);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        textField.setOnAction(event -> {
            selectedSubject.setText(textField.getText());
        });
        drawPane.setOnMouseClicked(event -> {
            if (event.getPickResult().getIntersectedNode() == drawPane) {
                if (selectedSubject != null) {
                    selectedSubject.setSelected(false);
                    selectedSubject = null;
                }
            }
        });
    }
}
